package com.boc.langxiang.presenter.common;

import android.widget.Toast;

import com.boc.langxiang.App;
import com.boc.langxiang.contract.common.MainContract;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.UserInfo;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MainPresenter implements MainContract.Presenter {

    private MainContract.View mView;
    private ApiService mService;

    public MainPresenter(MainContract.View view) {
        this.mView = view;
        mService = NetClient.getInstance().net().create(ApiService.class);
    }

    @Override
    public void getuser() {
        Observable<BaseResponse<UserInfo>> getuser = mService.getuser(UserInfoManager.getInstance().getToken());
        getuser.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<UserInfo>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<UserInfo> response) {
                        UserInfo userInfo = response.getContent();
                        UserInfoManager.getInstance().setUserInfo(userInfo);
                    }
                });
    }
}
